package com.zzl.alligator.management.system.controller;

import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysDictType;
import com.zzl.alligator.management.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典类型表(SysDictType)控制层
 *
 * @author zhouziliang
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 分页查询所有数据
     *
     * @param sysDictType 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(SysDictType sysDictType) {
        startPage();
        List<SysDictType> sysDictTypeList = sysDictTypeService.findSysDictTypeList(sysDictType);
        return JsonResult.success(getDataTable(sysDictTypeList));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param dictId 主键
     * @return 单条数据
     */
    @GetMapping("/{dictId}")
    public JsonResult findById(@PathVariable Long dictId) {
        return JsonResult.success(sysDictTypeService.findSysDictTypeById(dictId));
    }

    /**
     * 新增数据
     *
     * @param sysDictType 实体对象
     * @return 新增结果
     */
    @PostMapping
    public JsonResult add(@RequestBody SysDictType sysDictType) {
        return toAjax(this.sysDictTypeService.insertSysDictType(sysDictType));
    }

    /**
     * 修改数据
     *
     * @param sysDictType 实体对象
     * @return 修改结果
     */
    @PutMapping
    public JsonResult update(@RequestBody SysDictType sysDictType) {
        return toAjax(this.sysDictTypeService.updateSysDictType(sysDictType));
    }

    /**
     * 删除数据
     *
     * @param dictId 主键
     * @return 删除结果
     */
    @DeleteMapping("/{dictId}")
    public JsonResult delete(@PathVariable Long dictId) {
        SysDictType dictType = sysDictTypeService.findSysDictTypeById(dictId);
        if(dictType == null) {
            return JsonResult.success();
        }

        return toAjax(this.sysDictTypeService.deleteById(dictType.getDictType(),dictId));
    }

    @DeleteMapping("/clearCache")
    public JsonResult clearCache() {
        // @TODO 清除缓存
        return JsonResult.success();
    }
}